<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 24.3</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="24.2.3.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="24.3.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P4">Part IV. The C API</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#24">Chapter 24. An Overview of the C API</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h2>24.3 &ndash; Error Handling with the C API</h2>

<p>Unlike C++ or Java,
the C language does not offer an exception handling mechanism.
To ameliorate this difficulty,
Lua uses the <code>setjmp</code> facility from C,
which results in a mechanism similar to exception handling.
(If you compile Lua with C++,
it is not difficult to change the code so that it uses
real exceptions instead.)

<p>All structures in Lua are dynamic:
They grow as needed, and eventually shrink again when possible.
That means that the possibility of a memory-allocation failure
is pervasive in Lua.
Almost any operation may face this eventuality.
Instead of using error codes for each operation in its API,
Lua uses exceptions to signal these errors.
That means that almost all API functions may throw an error
(that is, call <code>longjmp</code>) instead of returning.

<p>When we write library code
(that is, C functions to be called from Lua),
the use of long jumps is almost as convenient as a
real exception-handling facility,
because Lua catches any occasional error.
When we write application code
(that is, C code that calls Lua), however,
we must provide a way to catch those errors.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="24.3.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

